// pages/settings/settings.js
Page({
  data: {
    solveSpeed: 100,
    difficulty: 'medium',
    difficultyText: '中等',
    showAnimation: true,
    showErrorHint: true,
    difficultyPickerVisible: false,
    difficultyValue: ['medium'],
    difficultyOptions: [
      { label: '简单', value: 'easy' },
      { label: '中等', value: 'medium' },
      { label: '困难', value: 'hard' }
    ]
  },

  onLoad() {
    this.loadSettings();
  },

  /**
   * 加载设置
   */
  loadSettings() {
    const settings = wx.getStorageSync('gameSettings') || {};
    
    this.setData({
      solveSpeed: settings.solveSpeed || 100,
      difficulty: settings.difficulty || 'medium',
      difficultyText: this.getDifficultyText(settings.difficulty || 'medium'),
      showAnimation: settings.showAnimation !== false,
      showErrorHint: settings.showErrorHint !== false,
      difficultyValue: [settings.difficulty || 'medium']
    });
  },

  /**
   * 保存设置
   */
  saveSettings() {
    const settings = {
      solveSpeed: this.data.solveSpeed,
      difficulty: this.data.difficulty,
      showAnimation: this.data.showAnimation,
      showErrorHint: this.data.showErrorHint
    };
    
    wx.setStorageSync('gameSettings', settings);
    
    // 通知主页面更新设置
    const pages = getCurrentPages();
    if (pages.length > 1) {
      const prevPage = pages[pages.length - 2];
      if (prevPage.route === 'pages/index/index') {
        prevPage.setData({
          solveSpeed: this.data.solveSpeed,
          difficulty: this.data.difficulty
        });
      }
    }
  },

  /**
   * 求解速度改变
   */
  onSpeedChange(e) {
    this.setData({
      solveSpeed: e.detail.value
    });
    this.saveSettings();
  },

  /**
   * 显示难度选择器
   */
  showDifficultyPicker() {
    this.setData({
      difficultyPickerVisible: true
    });
  },

  /**
   * 难度确认
   */
  onDifficultyConfirm(e) {
    const difficulty = e.detail.value[0];
    this.setData({
      difficulty: difficulty,
      difficultyText: this.getDifficultyText(difficulty),
      difficultyValue: [difficulty],
      difficultyPickerVisible: false
    });
    this.saveSettings();
  },

  /**
   * 选择器取消
   */
  onPickerCancel() {
    this.setData({
      difficultyPickerVisible: false
    });
  },

  /**
   * 动画开关改变
   */
  onAnimationChange(e) {
    this.setData({
      showAnimation: e.detail.value
    });
    this.saveSettings();
  },

  /**
   * 错误提示开关改变
   */
  onErrorHintChange(e) {
    this.setData({
      showErrorHint: e.detail.value
    });
    this.saveSettings();
  },

  /**
   * 清除游戏记录
   */
  clearHistory() {
    wx.showModal({
      title: '确认清除',
      content: '确定要清除所有游戏记录吗？此操作不可恢复。',
      confirmText: '清除',
      confirmColor: '#fa4350',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('gameHistory');
          wx.removeStorageSync('gameStatistics');
          wx.showToast({
            title: '已清除记录',
            icon: 'success'
          });
        }
      }
    });
  },

  /**
   * 关于应用
   */
  goToAbout() {
    wx.showModal({
      title: '数独求解器',
      content: '版本：v1.0.0\n\n采用DFS回溯算法\n可视化展示求解过程\n\n开发者：您的名字',
      showCancel: false,
      confirmText: '知道了'
    });
  },

  /**
   * 获取难度文本
   */
  getDifficultyText(difficulty) {
    const map = {
      'easy': '简单',
      'medium': '中等',
      'hard': '困难'
    };
    return map[difficulty] || '中等';
  },

  /**
   * 返回
   */
  goBack() {
    wx.navigateBack();
  }
});
